<template>
  <div class="my" v-if="userInfo !== ''">
    <div class="top">
      <div class="top1">
        <div class="left">
          <div class="name">{{ $store.state.userInfo.nickname }}</div>
          <div class="intro">{{ userInfo.intro }}</div>
        </div>
        <img
          v-if="userInfo.avatar"
          class="right"
          :src="baseUrl + userInfo.avatar"
          alt=""
        />
        <img v-else class="right" src="@/assets/logo.png" alt="" />
      </div>
      <ul class="top2">
        <li class="top2-item">
          <div class="num">298</div>
          <div class="txt">累计答题</div>
        </li>
        <li class="top2-item">
          <div class="num">98</div>
          <div class="txt">收藏题目</div>
        </li>
        <li class="top2-item">
          <div class="num">198</div>
          <div class="txt">我的错题</div>
        </li>
        <li class="top2-item">
          <div class="num">76%</div>
          <div class="txt">正确率</div>
        </li>
      </ul>
      <div class="top3">
        <!-- 组件传值
          1:ref
          2:props与emit
            父传子属性
              1:在子组件标签上定义一个属性  属性名=值
              2:子组件内接收:  定义一个props:["属性名"]

            子触发父方法
              1:在子组件标签上绑定一个方法 @子组件方法名="父组件方法"
              2:在子组件内触发父组件方法  this.$emit("子组件方法名",参数)
           -->
        <myCell
          class="r8"
          icon="&#xe64d;"
          title="我的岗位"
          :value="userInfo.position"
        ></myCell>
      </div>
    </div>
    <div class="middle">
      <div class="middle-content">
        <h3 class="middle-title">面经数据</h3>
        <ul class="middle-data">
          <li class="data-item">
            <div class="item1">
              昨日阅读<span class="color"
                >+{{
                  userInfo.shareData.read
                    ? userInfo.shareData.read.yesterday
                    : 0
                }}</span
              >
            </div>
            <div class="item2">17</div>
            <div class="item3">阅读总数</div>
          </li>
          <li class="data-item">
            <div class="item1">昨日获赞<span class="color">+300</span></div>
            <div class="item2">17</div>
            <div class="item3">获赞总数</div>
          </li>
          <li class="data-item">
            <div class="item1">昨日新增<span class="color">+300</span></div>
            <div class="item2">17</div>
            <div class="item3">新增总数</div>
          </li>
        </ul>
      </div>
    </div>
    <div class="bottom">
      <myCell
        class="bottom1"
        icon="&#xe655;"
        title="我的面经分享"
        value="21"
      ></myCell>
      <myCell icon="&#xe656;" title="我的消息" value="21"></myCell>
      <myCell icon="&#xe654;" title="收藏的题库" value="21"></myCell>
      <myCell icon="&#xe648;" title="收藏的企业" value="21"></myCell>
      <myCell icon="&#xe64f;" title="我的错题" value="21"></myCell>
      <myCell
        class="bottom6"
        icon="&#xe649;"
        title="收藏的面试经验"
        value="21"
      ></myCell>
    </div>
  </div>
</template>
<script>
// 组件套用组件  1:导入 2:注册  3:当标签使用
import myCell from './myCell'
// mapState属性   1:导入  import {mapState} from 'vuex'   2:在计算属性中定义  computed:{  ...mapState(["state中的属性"])}
import { mapState } from 'vuex'
import { getLocal } from '@/utils/local.js'
import { auInfo } from '@/api/my.js'
export default {
  computed: {
    //   userInfo就相当于vuex是的userInfo
    // userInfo () {
    //   return this.$store.state.userInfo
    // },
    // isLogin () {
    //   return this.$store.state.isLogin
    // }
    ...mapState(['userInfo', 'isLogin'])
  },
  data () {
    return {
      baseUrl: process.env.VUE_APP_URL
    }
  },
  created () {
    /*
    如果已登陆
       不做处理
    如果没有登陆(没有用户信息)
        如果有token
           通过token取到用户信息
        如果没有token
           跳转到登陆页面      
    
    */
    if (!this.isLogin) {
      if (getLocal('token')) {
        // 获取用户信息
        auInfo().then(res => {
          // 存储用户信息
          this.$store.commit('setUserInfo', res.data.data)
          // 设置用户登陆状态
          this.$store.commit('setIsLogin', true)
        })
      } else {
        this.$router.push('/login')
      }
    }
  },
  components: {
    myCell
  }
  /*
计算属性:
   使用场景:依赖一个或者多个值产生一个新的值
    特点:它产生的新值可以缓存下来,它可以当属性使用
   使用方法:
      computed:{
          方法名(){
              return 值
          }
      }
*/
}
</script>
<style lang="less" scoped>
.my {
  background-color: #f7f4f5;
  //   padding-bottom: 60px;
  .top {
    padding: 31px @p15 0;
    background-color: #c2002e;
    position: relative;
    .top1 {
      display: flex;
      //   水平方向左右对齐,中间平均分布
      justify-content: space-between;
      .right {
        width: 50px;
        height: 50px;
        border-radius: 50%;
        border: 3px solid rgba(255, 255, 255, 0.38);
      }
      .left {
        .name {
          font-size: 21px;
          font-family: PingFangSC, PingFangSC-Semibold;
          font-weight: 600;
          text-align: left;
          color: #ffffff;
          line-height: 30px;
          letter-spacing: 0px;
        }
        .intro {
          opacity: 0.7;
          font-size: 12px;
          font-family: PingFangSC, PingFangSC-Regular;
          font-weight: 400;
          text-align: left;
          color: #ffffff;
          line-height: 16px;
          letter-spacing: 0px;
        }
      }
    }
    .top2 {
      display: flex;
      justify-content: space-between;
      .top2-item {
        text-align: center;
        margin-top: 31px;
        padding-bottom: 55px;
        .num {
          font-size: 16px;
          font-family: DINAlternate, DINAlternate-Bold;
          font-weight: 700;
          text-align: center;
          color: #ffffff;
          line-height: 24px;
          letter-spacing: 0px;
        }
        .txt {
          opacity: 0.7;
          font-size: 12px;
          font-family: PingFangSC, PingFangSC-Regular;
          font-weight: 400;
          text-align: center;
          color: #ffffff;
          line-height: 16px;
          letter-spacing: 0px;
        }
      }
    }
    .top3 {
      position: absolute;
      left: 0;
      bottom: -16px;
      width: 100%;
      padding: 0 @p15;
    }
  }
  .middle {
    padding: 26px @p15 10px;
    .middle-content {
      padding: 19px 15px 16px;
      background-color: #fff;
      .middle-title {
        font-size: 14px;
        font-family: PingFangSC, PingFangSC-Semibold;
        font-weight: 600;
        color: #181a39;
        line-height: 20px;
        letter-spacing: 0px;
        margin-bottom: 17px;
      }
      .middle-data {
        display: flex;
        justify-content: space-between;
        .data-item {
          text-align: center;
          .item1 {
            font-size: 12px;
            font-family: PingFangSC, PingFangSC-Regular;
            font-weight: 400;
            text-align: center;
            color: #b4b4bd;
            line-height: 14px;
            letter-spacing: 0px;
            .color {
              color: #00b8d4;
            }
          }
          .item2 {
            margin: 10px 0;
            font-size: 20px;
            font-family: DINAlternate, DINAlternate-Bold;
            font-weight: 700;
            text-align: center;
            color: #181a39;
            line-height: 24px;
            letter-spacing: 0px;
          }
          .item3 {
            font-size: 12px;
            font-family: PingFangSC, PingFangSC-Regular;
            font-weight: 400;
            text-align: center;
            color: #545671;
            line-height: 16px;
            letter-spacing: 0px;
          }
        }
      }
    }
  }
  .bottom {
    padding: 0 @p15 10px;
  }
  .bottom1 {
    border-top-left-radius: 8px;
    border-top-right-radius: 8px;
  }
  .bottom6 {
    border-bottom-left-radius: 8px;
    border-bottom-right-radius: 8px;
  }
  .r8 {
    border-radius: 8px;
  }
}
</style>
